package com.qf.openapi.web.master.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.openapi.web.master.dao.IAdminUserDao;
import com.qf.openapi.web.master.entity.AdminUser;
import com.qf.openapi.web.master.service.IAdminUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.List;

@Service("iAdminUserService")
public class IAdminUserServiceImpl implements IAdminUserService {
    @Autowired
    private IAdminUserDao iAdminUserDao;

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public Boolean getLogin(String username, String password) {
        AdminUser user = iAdminUserDao.login(username);
        if (ObjectUtils.isEmpty(user)) {
            return false;
        }
        String passWord = user.getPassword();
        if (passWord.equals(passWord)) {
            return true;
        }
        return false;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public PageInfo<AdminUser> getByUsers(Integer pageNum, Integer pageSize, String realName, String email, String status) {

        PageHelper.startPage(pageNum, pageSize);
        List<AdminUser> list = iAdminUserDao.getAllUsers(realName, email, status);
        return new PageInfo<AdminUser>(list);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean delUser(Integer[] ids) {
        if (ids == null) {
            return false;
        }
        Integer integer = iAdminUserDao.delAdminUser(ids);
        return integer >= 1 ? true : false;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean addUser(AdminUser user) {
        if (ObjectUtils.isEmpty(user)) {
            return false;
        }
        Integer integer = iAdminUserDao.addAdminUser(user);
        return integer >= 1;

    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean updateUser(AdminUser user) {
        if (ObjectUtils.isEmpty(user)) {
            return false;
        }
        Integer integer = iAdminUserDao.updateAdminUser(user);
        if (integer >= 1) {
            return true;
        } else {
            return false;
        }

    }
}
